import json
import re

import requests
from visual.DB import Db


class GetData:
    url = "https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner"
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
        "referer": "https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banne"
    }
    resp = requests.get(url, headers=headers)  # requests GET请求
    # <Response [200]> 访问成功
    resp.close()

    # 获取数据
    html_data = resp.text
    # 解析数据  正则
    result_data = re.findall('"component":\[(.*)\],', html_data)[0]

    # 转化类型
    json_data = json.loads(result_data)
    caseList = json_data['caseList']

    # 导入封装DB模块，将数据存储到Mysql数据库
    db = Db(host='localhost', user='root', password='123456', charset='utf8', db='django_project')

    # 每次插入前先清空表
    del_sql = "truncate visual_projectdata"
    db.others(del_sql)
    areas = []
    citylist = []
    # 循环得到的数据存入数据库
    for case in caseList:
        areas.append(case['area'])
        area = case['area']  # 省份
        curConfirm = case['curConfirm']  # 确诊人数
        confirmedRelative = case['confirmedRelative']  # 新增人数
        confirmed = case['confirmed']  # 累计确诊
        crued = case['crued']  # 累计治愈
        died = case['died']  # 累计死亡
        citylist.append(case['subList'])
        # 插入语句
        insert_data_sql = "insert into visual_projectdata values (%s,%s,%s,%s,%s,%s);"
        db.insert_many(insert_data_sql, [area, curConfirm, confirmedRelative, confirmed, crued, died])
        db.close()  # 数据库连接关闭
        # print(area, curConfirm, confirmed, confirmedRelative, crued, died)
    citys_data = []
    # print(areas)
    # print(citylist)
    for index, item in enumerate(citylist):
        for city in item:
            if city['city'] == '待确认人员' or city['city'] == '境外输入' or city['city'] == '外地来湘人员' or city['city'] == '待确认' or \
                    city['city'] == '外地来津人员' or city['city'] == '外地来沪人员' or city['city'] == '外地来京人员':
                break
            else:
                # print(city['died'])
                if city['died'] == ' ':
                    citys_data.append(
                        {'area': areas[index], 'city': city['city'] + '市', 'curConfirm': city['curConfirm'],
                         'confirmedRelative': city['confirmedRelative'], 'confirmed': city['confirmed'],
                         'cured': city['crued'], 'died': city['died']})
                else:

                    # 省份  城市    # 确诊人数   # 新增人数 # 累计确诊 # 累计治愈 # 累计死亡
                    citys_data.append(
                        {'area': areas[index], 'city': city['city'] + '市', 'curConfirm': city['curConfirm'],
                         'confirmedRelative': city['confirmedRelative'], 'confirmed': city['confirmed'],
                         'cured': city['crued'], 'died': city['died']})  # 省份

    # print(citys_data)
    del_sql = "truncate visual_citydata"
    db.others(del_sql)
    for data in citys_data:
        insert_data_sql = "insert into visual_citydata values (%s,%s,%s,%s,%s,%s,%s);"
        db.insert_many(insert_data_sql,
                       [data['area'], data['city'], data['curConfirm'], data['confirmedRelative'], data['confirmed'],
                        data['cured'], data['died']])
        db.close()  # 数据库连接关闭
    # print(areas)
    # print(citylist)
    dangerousAreasList = []
    for case in citylist:
        for data in case:
            # print(data['dangerousAreas'])
            for index, item in enumerate(data['dangerousAreas']['subList']):
                dangerousAreasList.append({'area': item['area'], 'level': item['level']})
    del_sql = "truncate visual_dangerousareas"
    db.others(del_sql)
    for index, dangerousArea in enumerate(dangerousAreasList):
        insert_data_sql = "insert into visual_dangerousareas values (%s,%s,%s);"
        db.insert_many(insert_data_sql, [int(index + 1), dangerousArea['area'], dangerousArea['level']])
        db.close()  # 数据库连接关闭

